<div class="container">			
	<!--<ol class="breadcrumb">
		<li><a href="#">人力资源管理系统</a></li>
		<li><a href="#">员工管理</a></li>				
		<li>修改密码</li>
	</ol>-->
	
	<h2 class="page-header">修改密码</h2>
	
	<form id="form1" class="form-horizontal">				
		<div class="form-group">
			<label for="" class="col-sm-2">旧密码</label>
			<div class="col-sm-8">
				<input type="password" id="oldPwd" name="oldPwd" placeholder="请输入旧密码" class="form-control"/>
			</div>
		</div>	
		<div class="form-group">
			<label for="" class="col-sm-2">请输入新密码</label>
			<div class="col-sm-8">
				<input type="password" id="userPwd" name="userPwd" placeholder="请输入新密码" class="form-control"/>
			</div>
		</div>
		<div class="form-group">
			<label for="" class="col-sm-2">请再次输入新密码</label>
			<div class="col-sm-8">
				<input type="password" id="userPwd2" name="userPwd2" placeholder="请再次输入新密码" class="form-control"/>
			</div>
		</div>
		<div class="form-group">
			<div class="col-sm-offset-2 col-sm-8">
				<input type="button" value="修改密码" class="btn btn-success" id="updateBtn"/>
				<input type="button" value="取消" class="btn btn-warning" onclick="history.back();" />
			</div>					
		</div>
	</form>			
</div>		

<script type="text/javascript" src="js/jquery.js" ></script>
<script type="text/javascript" src="js/global.js" ></script>
<script type="text/javascript" src="js/bootstrap/js/bootstrap.js" ></script>
<script type="text/javascript" src="js/layer/layer.js" ></script>
<script>
	
	$(function() {
		
		//表单验证
		function checkForm() {
			
			var oldPwd = $('#oldPwd');
			var userPwd = $('#userPwd');
			var userPwd2 = $('#userPwd2');
			
			if (oldPwd.val() == '' || oldPwd.val().length > 12) {
				layer.msg('旧密码不能为空且最多12位');
				oldPwd.focus();
				return false;
			}
			
			//判断旧密码是否正确（同步请求）
			var result;
			
			$.ajax({
				type:"post",
				url:baseUrl + "/user/checkPwd",
				async:false,
				data:{userPwd:$('#oldPwd').val()},
				dataType:'text'
			})
			.done(function(data){
				result = data;
			})
			.fail(function(){
				layer.msg('检查旧密码请求失败');
			});
			
			if (result == 'false') {
				layer.msg('旧密码不正确');
				oldPwd.focus();
				return false;
			}					
			
			if (userPwd.val() == '' || userPwd.val().length > 12) {
				layer.msg('新密码不能为空且最多12位');
				userPwd.focus();
				return false;
			}
			
			if (userPwd.val() != userPwd2.val()) {
				layer.msg('两次密码输入不一致！');
				userPwd2.focus();
				return false;
			}
			
			return true;										
		}	
										
			
		//修改按钮单击事件回调
		$('#updateBtn').click(function(){
			
			//调用表单验证
			if (!checkForm()) 
				return;
			
			//ajax请求
			$.post(baseUrl + '/user/updatePwd',{userPwd:$('#userPwd').val()},function(){
				layer.msg('修改密码成功，请重新登陆');
				
				setTimeout(function(){
					window.top.location.href = baseUrl + '/LogoutServlet';
				},500);
			})
			.error(function(){
				layer.msg('修改密码请求失败');
			});						
			
		});					
	});			
	
</script>		
